package com.example.v2.config;

import com.example.v2.intercepter.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SpringMVCConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // /** 匹配服务器的任意请求地址
        registry.addMapping("/**")
                .allowedHeaders("*")  //允许任意请求头访问
                .allowedMethods("*")  //允许任意请求方式 get/post/put/delete
                .allowedOriginPatterns("*")  //允许任意域访问
                .allowCredentials(true)  //允许携带cookie/session相关信息
                .maxAge(3600); //一小时内同一请求不再检查  直接放行
    }

    @Autowired
    private JwtInterceptor jwtInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePathList = new ArrayList<>();
        excludePathList.add("/sps/admin/**");
        excludePathList.add("/sps/shop/registerShops");
        excludePathList.add("/sps/upload/**");
        excludePathList.add("/images/**");
        excludePathList.add("");
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathList);
    }



}
